# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.

library(estimatr)
library(tidyverse)
# library(metafor)
source("helper_file.R")

# load data and uncount
aggregated_set <- read_rds(file = "aggregated_analysis_set.rds")
main_analysis_set <- uncount(aggregated_set, weights = n)

main_analysis_set <-
  main_analysis_set %>%
  mutate(condition = factor(condition, levels = c("control", "treat")))


fit_linear_early <- lm_robust(voted_early_2020 ~ condition*tss , weights = ipw, data = main_analysis_set)
fit_linear_inperson <- lm_robust(voted_in_person_2020 ~ condition*tss , weights = ipw, data = main_analysis_set)
fit_linear_voted <- lm_robust(voted_in_2020 ~ condition*tss , weights = ipw, data = main_analysis_set)

newdat <- expand_grid(condition = c("control", "treat"), tss = seq(30, 70))

preds_early <- predict(fit_linear_early, newdata = newdat, se.fit = TRUE, interval = "confidence")$fit
preds_inperson <- predict(fit_linear_inperson, newdata = newdat, se.fit = TRUE, interval = "confidence")$fit
preds_voted <- predict(fit_linear_voted, newdata = newdat, se.fit = TRUE, interval = "confidence")$fit


newdat_early <- bind_cols(newdat, data.frame(preds_early))
newdat_inperson <- bind_cols(newdat, data.frame(preds_inperson))
newdat_voted <- bind_cols(newdat, data.frame(preds_voted))

preds_df <-
  bind_rows(
    voted_early_2020 = newdat_early,
    voted_in_person_2020 = newdat_inperson,
    voted_in_2020 = newdat_voted,
    .id = "outcome"
  ) %>%
  mutate(outcome_label =
           factor(
             outcome,
             levels = c(
               "voted_early_2020",
               "voted_in_person_2020",
               "voted_in_2020",
               "voted_early_2020_feb"
             ),
             labels = c(
               "Voted early",
               "Voted in person",
               "Voted",
               "Voted early (preliminary records)"
             )
           ))

group_means <-
  main_analysis_set %>%
  group_by(condition, tss_round) %>%
  do(tidy(lm_robust(
    cbind(voted_in_person_2020, voted_early_2020, voted_in_2020) ~ 1, 
    weights = ipw, data = .
  )))%>%
  mutate(outcome_label =
           factor(
             outcome,
             levels = c(
               "voted_early_2020",
               "voted_in_person_2020",
               "voted_in_2020",
               "voted_early_2020_feb"
             ),
             labels = c(
               "Voted early",
               "Voted in person",
               "Voted",
               "Voted early (preliminary records)"
             )
           ))

label_df <-
  tibble(
    label = c("Control group", "Treatment group"),
    tss_round = c(60, 60),
    estimate = c(0.31, 0.24),
    condition = c("control", "treat"),
    outcome_label = factor("Voted early"))


blank_df <-
  tibble(
    outcome_label = factor(rep(c("Voted early",
                             "Voted in person",
                             "Voted"), each = 2)),
    estimate = c(0.35,0.20, 0.35, 0.20, 0.60, 0.45),
    tss_round = 50,
    condition = "treat"
  )


# this is a hack for pseudo coord_cartesian
group_means <-
  group_means |> 
  mutate(conf.high = if_else(conf.high >= 0.60, 0.60, conf.high))


g <-
  ggplot(group_means, aes(tss_round, estimate, shape = condition)) +
  geom_blank(data = blank_df) +
  geom_linerange(
    data = filter(group_means, condition == "control"),
    aes(ymin = conf.low, ymax = conf.high),
    color = gray(0.9)
  ) +
  geom_point(data = filter(group_means, condition == "control"),
             color = gray(0.7)) +
  geom_line(
    data = filter(preds_df, condition == "control"),
    aes(y = fit, x = tss),
    color = gray(0.7)
  ) +
  geom_ribbon(
    data = filter(preds_df, condition == "control"),
    aes(
      y = fit,
      ymin = lwr,
      ymax = upr,
      x = tss,
      fill = condition
    ),
    color = gray(0.7),
    fill = gray(0.7),
    alpha = 0.5
  ) +
  geom_linerange(
    data = filter(group_means, condition == "treat"),
    aes(ymin = conf.low, ymax = conf.high),
    color = gray(0.5)
  ) +
  geom_point(data = filter(group_means, condition == "treat"),
             color = gray(0.2)) +
  geom_line(
    data = filter(preds_df, condition == "treat"),
    aes(y = fit, x = tss),
    color = gray(0.2)
  ) +
  geom_ribbon(
    data = filter(preds_df, condition == "treat"),
    aes(
      y = fit,
      ymin = lwr,
      ymax = upr,
      x = tss,
      fill = condition
    ),
    color = gray(0.2),
    fill = gray(0.1),
    alpha = 0.5
  ) +
  geom_label(data = filter(label_df, condition == "treat"), aes(label = label), color = "black") +
  geom_label(data = filter(label_df, condition == "control"), aes(label = label), color = gray(0.6)) +
  facet_wrap( ~ outcome_label, scales="free_y") +
  theme_minimal() +
  labs(y = "Fraction voting",
       x = "Trump support score") +
  scale_shape_manual(name = "Condition",
                     labels = c("Control", "Treatment"),
                     values = c(19, 17)) +
  scale_colour_manual(name = "Condition",
                      aesthetics = c("colour", "fill"),
                      labels = c("Control", "Treatment"),
                      values = c('light gray', 'black' ) ) +
  # guides(fill = guide_legend(override.aes = list(shape = c(19, 17)))) +
  theme(legend.position = "none")
  

g

#theme(legend.position = "none")

ggsave("output/figure_3.pdf", plot = g, width = 8.5, height = 4.5)

